﻿/// <reference path="ADS.js" />

ADS.addEvent(window, 'load', topClick);
function topClick() {
    var divs = ADS.getElementByClassName('right_top', 'div');
    for (var i = 0; i < divs.length; i++) {
        divs[i].title = 'Double Click to Maximise';
        ADS.addEvent(divs[i], 'dblclick', function () {
            var topFrame = window.parent.document.getElementById("topFrame");
            var mainFrame = window.parent.document.getElementById("mainFrame");
            if (this.title == 'Double Click to Maximise') {
                this.title = 'Double Click to Minimize';
                topFrame.rows = '0,*';
                mainFrame.cols = '0,*';
            }
            else {
                this.title = 'Double Click to Maximise';
                topFrame.rows = '105,*';
                mainFrame.cols = '206,*';
            }
        });
    }
}
/*----数据表前的checkBox控制----*/
ADS.addEvent(window, 'load', checkEvent);
function checkEvent() {
    var all = ADS.getElementByClassName('check_all');
    var item = ADS.getElementByClassName('check_item');
    for (var i = 0; i < all.length; i++) {
        ADS.addEvent(all[i], 'click', function () {
            for (var j = 0; j < item.length; j++) {
                if (!item[j].disabled) {
                    item[j].checked = this.checked;
                }
            }
        });
    }
    //需要获取列表的button
    var checklist = ADS.getElementByClassName('checklist');
    for (var i = 0; i < checklist.length; i++) {
        ADS.addEvent(checklist[i], 'click', function (W3CEvent) {
            var item = ADS.getElementByClassName('check_item', 'input');
            var value = '';
            for (var i = 0; i < item.length; i++) {
                if (item[i].checked) {
                    value += item[i].value + ',';
                }
            }
            if (value.length == '') {
                alert('No items are selected!!!');
                ADS.preventDefault(W3CEvent);
            }
            else {
                value = value.substr(0, value.length - 1);
                ADS.$('hfchecklist').value = value;
            }
        });
    }
    //数字输入验证
    var inputs = ADS.getElementByClassName('decimal', 'input');
    for (var i = 0; i < inputs.length; i++) {
        ADS.addEvent(inputs[i], 'keyup', function () {
            if (isNaN(this.value) || this.value.indexOf(' ') != -1) {
                this.value = '';
            }
        });
    }
}


/*-----------------------Button Click-------------------------*/
ADS.addEvent(window, 'load', btnClick);
function btnClick() {
    ADS.addEvent(ADS.$('ddlClient'), 'change', function () {
        if (this.value != '--Select Client--') {
            ADS.$('newOrder_BtnNextA').disabled = false;
        }
        else {
            ADS.$('newOrder_BtnNextA').disabled = true;
        }
    });
    /*--NewOrder---Default--*/
    ADS.addEvent(ADS.$('newOrder_BtnNextA'), 'click', function (W3CEvent) {
        if (ADS.$('refNo').value == '') {
            alert('RefNO Can Not Be Empty!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('check_refNo').innerHTML.indexOf('Exists') !=-1 ) {
            alert('RefNo '+ADS.$('refNo').value+' is exists!');
            ADS.preventDefault(W3CEvent);
        }
    });
    ADS.addEvent(ADS.$('newOrder_BtnNextB'), 'click', function (W3CEvent) {
        if (ADS.$('refNo').value == '') {
            alert('RefNO Can Not Be Empty!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('clientName').value == '') {
            alert('You Must Input The Name of Your New Client!');
            ADS.preventDefault(W3CEvent);
        }
        else if(ADS.$('check_clientname').innerHTML.indexOf('Exists')!=-1){
            alert('Client Name '+ADS.$('clientName').value+' is exists!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('monoName').value == '') {
            if (!window.confirm('This Client Will Never Use Any MonoName?')) {
                ADS.$('monoName').focus();
                ADS.preventDefault(W3CEvent);
            }
        }
    });
    /*--ClientPosture--*/
    ADS.addEvent(ADS.$('ClientPosture_BtnNext'), 'click', function (W3CEvent) {
        var ckbs = ADS.getElementByClassName('ckb_posture', 'input');
        var result = '';
        for (var i = 0; i < ckbs.length; i++) {
            if (ckbs[i].checked == true) {
                result += ckbs[i].id + ',';
            }
        }
        if (result.length == 0) {
            if (!window.confirm('If no picture is selected,We will cut as normal posture!Are You Sure?')) {
                ADS.preventDefault(W3CEvent);
            }
        }
        else {
            result = result.substr(0, result.length - 1);
            ADS.$("idlist").value = result;
        }
    })
    ADS.addEvent(ADS.$('PostureData_btnAdd'), 'click', function (W3CEvent) {
        if (ADS.$('postureName').value == '') {
            alert('The posture name can not be empty!');
            ADS.preventDefault(W3CEvent);
        }
    });
    /*--OrderItemList--*/
    ADS.addEvent(ADS.$("addNewItem"), 'click', function (W3CEvent) {
        if (ADS.$("ddlItem").value == '0') {
            alert('Pls select a item!');
            ADS.preventDefault(W3CEvent);
        }
    });
    ADS.addEvent(ADS.$('ItemStyle_btnNext'), 'click', function (W3CEvent) {
        var values = ADS.getElementByClassName('style_value','input');
        var mark = true;
        for (var i = 0; i < values.length; i++) {
            if (values[i].value == "UnSelected"||values[i].value=='') {
                mark = false;
                alert("Pls finish your design!");
                var id = values[i].id.substring(6, values[i].id.length);
                showStyle(id);
                ADS.preventDefault(W3CEvent);
                break;
            }
        }
        //汇总样式数据
        if (mark) {
            var keyvalue = '';
            for (var i = 0; i < values.length; i++) {
                keyvalue += values[i].id.substr(6, values[i].id.length) + '=' + values[i].title + '=' + values[i].value + '$';
            }
            if (keyvalue.length != 0) {
                keyvalue = keyvalue.substr(0, keyvalue.length - 1);
            }
            ADS.$('hfKeyValue').value = keyvalue;
        }
    });
  /*------Message-----*/
    ADS.addEvent(ADS.$('addMessage'), 'click', function (W3CEvent) {
        if (ADS.$('tbReceiver').value == '') {
            alert('Pls input the receiver!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('tbTitle').value == '') {
            alert('The title can not be empty!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('tbContent').value == 'a') {
            alert('Can not send a empty message!');
            ADS.preventDefault(W3CEvent);
        }
    });
    /*------Style Add-----*/
    ADS.addEvent(ADS.$('Style_Add'), 'click', function (W3CEvent) {
        if (ADS.$('styleName').value == '') {
            alert('Pls input the Style Name!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('cnName').value == '') {
            alert('Pls input the CN Name!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('ddlGender').value == '0') {
            alert('Pls select the gender of the style!');
            ADS.preventDefault(W3CEvent);
        }
    });
    ADS.addEvent(ADS.$('userAdd_btnNext'), 'click', function (W3CEvent) {
        if (ADS.$('ddlRole').value == 0) {
            alert('Pls select the role!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('userName').value == '') {
            alert('UserName can not be empty!')
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('password').value == '') {
            alert('Passwork can not be empty!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('ddlPay').value == '0') {
            alert('Pls select a default paymethod!')
            ADS.preventDefault(W3CEvent);
        }
        else if(ADS.$('ddlShip').value=='0')
        {
            alert('Pls select a default shipmethod!');
            ADS.preventDefault(W3CEvent);
        }
    });
    //添加样式ID到ruleCart
    var imgs = ADS.getElementByClassName('ruleCart', 'img');
    for (var i = 0; i < imgs.length; i++) {
        ADS.addEvent(imgs[i], 'click', function () {
            var value = ADS.Cookies.get('ruleCart');
            value = value == '' ? this.id : value + ',' + this.id;
            ADS.Cookies.set('ruleCart', value);
            this.style.display = 'none';
        });
    }
    //RoleManager
    ADS.addEvent(ADS.$('RoleManager_btnAdd'), 'click', function (W3CEvent) {
        if (ADS.$('roleName').value == '') {
            alert('Pls input the Role Name!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('startPage').value == '') {
            alert('Pls input the startPage');
            ADS.preventDefault(W3CEvent);
        }
    });
    ADS.addEvent(ADS.$('menuList'), 'click', function () {
        openDialog('/Manager/AccountManager/SelectMenu.aspx',300,500,200,200);
    });
    //Ship Ment
    ADS.addEvent(ADS.$('ShipMent_btnShip'),'click',function(W3CEvent){
        if(ADS.$('trackNo').value=='')
        {
            alert('Pls input the tracking  No!');
            ADS.preventDefault(W3CEvent);
        }
    });
    //Cloth Add
    ADS.addEvent(ADS.$('Cloth_btnAdd'), 'click', function (W3CEvent) {
        if (ADS.$('clothNo').value == '') {
            alert('Cloth No can not be empty!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('clothPrice').value == '') {
            alert('Pls input the price!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('clothColor').value == '') {
            alert('Pls input the cloth color!');
            ADS.preventDefault(W3CEvent);
        }
        else if (ADS.$('quality').value == '') {
            alert('Pls input the cloth quality!');
            ADS.preventDefault(W3CEvent);
        }
    });
}


/*-----------------------Next Button Click-------------------------*/
/*-------鼠标滑过效果------*/
ADS.addEvent(window, 'load', mouseEvent)
function mouseEvent() {
    var trs = ADS.getElementByClassName('tr_list_content', 'tr');
    for (var i = 0; i < trs.length; i++) {
        ADS.addEvent(trs[i], 'mouseover', function () {
            this.style.border = '1px solid #7FFF30';
            this.style.background = '#E2FECD';
        })
        ADS.addEvent(trs[i], 'mouseout', function () {
            this.style.border = 0;
            this.style.background = '';
        });
    }
}



/*------Item Cloth-----*/
ADS.addEvent(window, 'load', itemCloth);

function itemCloth() {
    //select Cloth
    var sel = ADS.getElementByClassName('cloth_select', 'a');
    for (var i = 0; i < sel.length; i++) {
        ADS.addEvent(sel[i], 'click', function (W3CEvent) {
            ADS.$('styleName').innerHTML = this.title;
            ADS.$('clothid').value = this.id;
            ADS.$('UpdatePanel1').style.display = 'block';
            ADS.preventDefault(W3CEvent);
        });
    }
}

//购买布料（发送邮件）
ADS.addEvent(window, 'load', clothBuy);
function clothBuy() {
    var adds = ADS.getElementByClassName('addremark', 'input');
    for (var i = 0; i < adds.length; i++) {
        ADS.addEvent(adds[i], 'click', function () {
            var id = this.id.substring(10, this.id.length);
            var remark = ADS.$('remark_' + id);
            if (remark.style.display == 'none') {
                remark.style.display = 'inline';
                ADS.addEvent(remark, 'keyup', function () {
                    var id = this.id.substring(7, this.id.length);
                    ADS.$('remark' + id).innerHTML = this.value;
                });
                remark.focus();
            }
            else {
                remark.style.display = 'none';
            }
        });
    }
    var sends = ADS.getElementByClassName('sendEmail', 'input');
    for (var i = 0; i < sends.length; i++) {
        ADS.addEvent(sends[i], 'click', function () {
            var id = this.name;
            var btn = this;
            var url = '/Manager/OrderManager/ashx/SendEmail.ashx?companyname=' + ADS.$('companyname_' + id).innerHTML;
            url += '&body=' + ADS.$('body_' + id).innerHTML + '&t=' + Math.random();
            ADS.$('remark_' + id).style.display = 'none';
            ADS.ajaxRequest(url, {
                loadListener: function () {
                    ADS.$('lb_' + id).innerHTML = '<span style="color:red">Sending...</span>';
                },
                completeListener: function () {
                    ADS.$('lb_' + id).innerHTML = '<span style="color:Green">' + this.responseText + '</span>';
                    btn.style.display = 'none';
                },
                errorListener: function () {
                    ADS.$('lb_' + id).innerHTML = '<span style="color:red" title="error message: ' + this.statusText + '">Send Fail!</span>';
                }
            })
        });
    }
    
}
/*--------Item Size-------*/
ADS.addEvent(window, 'load', itemSize)
function itemSize() {
    ADS.addEvent(ADS.$('ItemSize_btnNext'), 'click', function (W3CEvent) {
        var mustinputs = ADS.getElementByClassName('must', 'input');
        var mark = true;
        for (var i = 0; i < mustinputs.length; i++) {
            if (mustinputs[i].value == '') {
                mark = false;
                mustinputs[i].focus();
                alert('Pls Finish Your Input!')
                ADS.preventDefault(W3CEvent);
                break;
            }
        }
        if (mark) {
            var body = "";
            var finish = "";
            var inputs = ADS.getElementByClassName('in_size', 'input');
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].id == 'bodysize') {
                    body += inputs[i].name + '=' + inputs[i].value + '$';
                }
                else {
                    finish += inputs[i].name + '=' + inputs[i].value + '$';
                }
            }
            if (body != '') {
                body = body.substring(0, body.length - 1);
            }
            if (finish != '') {
                finish = finish.substring(0, finish.length - 1);
            }
            ADS.$('hfbody').value = body;
            ADS.$('hffinish').value = finish;
        }
    });
}

/*--------Order Manager------------------------*/
ADS.addEvent(window, 'load', showItem);
//显示订单项
function showItem() {
    var imgs = ADS.getElementByClassName('show_item', 'img');
    for (var i = 0; i < imgs.length; i++) {
        ADS.addEvent(imgs[i], 'click', function () {
            var itemlist = ADS.$('item_' + this.id);
            if (itemlist.style.display == "") {
                itemlist.style.display = 'none';
                this.src = '/images/showitem.gif';
            }
            else {
                itemlist.style.display = "";
                this.src = '/images/hideitem.gif';
            }
        });
    }
}
/*--------Style Manager------------*/

/*--------需要外部调用的函数------------------------*/
/*--Init PosturePicture--*/
function postureInit() {
    var idlist = ADS.$("idlist").value.split(',');
    var ckbs = ADS.getElementByClassName('ckb_posture', 'input');
    for (var j = 0; j < ckbs.length; j++) {
        ckbs[j].checked = false;
        for (var i = 0; i < idlist.length; i++) {
            if (ckbs[j].id == idlist[i]) {
                ckbs[j].checked = true;
            }
        }
    }
}

/*--Init ItemSize--*/
function sizeInit() {
    var body = ADS.$("hfbody").value;
    var finish = ADS.$("hffinish").value;
    var inputs = ADS.getElementByClassName('in_size', 'input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].id == 'bodysize') {
            var bodysize = body.split('$');
            for (var j = 0; j < bodysize.length; j++) {
                var size = bodysize[j].split('=');
                if (inputs[i].name == size[0]) {
                    inputs[i].value = size[1] == 0 ? "" : size[1];
                    break;
                }
            }
        }
        else {
            var finishsize = finish.split('$');
            for (var j = 0; j < finishsize.length; j++) {
                {
                    var size = finishsize[j].split('=');
                    if (inputs[i].name == size[0]) {
                        inputs[i].value = size[1] == 0 ? "" : size[1];
                        break;
                    }
                }
            }
        }
    }
}
/*--Init Item Style--*/


//open detail
function openDialog(href, width, height, top, left) {
    top = top || 0;
    left = left || 100;
    width = width || 595;
    height = height || 768;
    window.open(href, '', 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar=0,menubar=0,scrollbars=yes,resizable=0,location=no');
    return false;
}
//check RefNo
function checkRefNo(node) {
    var refNo = node.value;
    if (refNo != '') {
        var label = ADS.nextElement(node, 'label');
        ADS.ajaxRequest('/Order/ashx/checkInput.ashx?refNo=' + refNo, {
            completeListener: function () {
                if (this.responseText == 'OK') {
                    label.innerHTML = '<span style="color:green">' + this.responseText + '!</span>';
                }
                else {
                    label.innerHTML = '<span style="color:Red">' + this.responseText + '!</span>';
                }
            },
            errorListener: function () {
                label.innerHTML = '<span style="color:green">' + this.errorText + '!</span>';
            }
        });
    }
}
//check ClientName
function checkClientName(node) {
    var name = node.value;
    if (name != '') {
        var label = ADS.nextElement(node, 'label');
        ADS.ajaxRequest('/Order/ashx/checkInput.ashx?clientName=' + name, {
            completeListener: function () {
                if (this.responseText == 'OK') {
                    label.innerHTML = '<span style="color:green">' + this.responseText + '!</span>';
                }
                else {
                    label.innerHTML = '<span style="color:Red">' + this.responseText + '!</span>';
                }
            }
        });
    }
}